<?php


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class mobileplugin_nimba_addclick {
	function  __construct() {
		global $_G;
	    loadcache('plugin');
		$this->vars = $_G['cache']['plugin']['nimba_addclick'];
		$this->thread=intval($this->vars['thread']);
		$this->article=intval($this->vars['article']);
		$this->forums=unserialize($this->vars['forums']);
		$this->addnum=intval($this->vars['addnum']);
		$this->forumnum=trim($this->vars['forumnum']);
		$this->addrand=intval($this->vars['addrand']);
		$this->maxnum=intval($this->vars['maxnum']);
		$this->clearcache=intval($this->vars['clearcache']);
		$this->noreload=intval($this->vars['noreload']);
		$this->forumnum();
	}
	function forumnum(){
		$res=array();
		//free
		$this->forumnum=$res;
	}
	
	function doClick($id,$mod){
		global $_G;
		if(rand(1,100)>$this->addrand) return '';
		if($mod=='thread'){
			$add=isset($this->forumnum[$_G['fid']])? $this->forumnum[$_G['fid']]:$this->addnum;
			$this->_viewthread_updateviews($_G['forum_thread']['threadtableid'],$add);
		}elseif($mod=='article'){
			$add=$this->addnum;
			C::t('portal_article_count')->increase(array($id), array('viewnum'=>$add));
		}
	}
	
	function _viewthread_updateviews($tableid,$add) {
		global $_G;
		if(!$tableid&&!$add) return '';
		if(!$this->noreload || $_G['cookie']['nimba_addclick_viewid'] != 'tid_'.$_G['tid']) {//防刷新
			if(!$tableid && $_G['setting']['optimizeviews']) {
				if($_G['forum_thread']['addviews']) {
					if($_G['forum_thread']['addviews'] < 100) {
						C::t('forum_threadaddviews')->update_by_tid($_G['tid']);
					} else {
						if(!discuz_process::islocked('update_thread_view')) {
							$row = C::t('forum_threadaddviews')->fetch($_G['tid']);
							C::t('forum_threadaddviews')->update($_G['tid'], array('addviews' => 0));
							C::t('forum_thread')->increase($_G['tid'], array('views' => $row['addviews']+$add), true);
							discuz_process::unlock('update_thread_view');
						}
					}
				} else {
					C::t('forum_threadaddviews')->insert(array('tid' => $_G['tid'], 'addviews' => $add), false, true);
				}
			} else {
				C::t('forum_thread')->increase($_G['tid'], array('views' => $add), true, $tableid);
			}
		}
		dsetcookie('nimba_addclick_viewid', 'tid_'.$_G['tid']);
	}
}

class mobileplugin_nimba_addclick_forum extends mobileplugin_nimba_addclick{
	function viewthread_top_mobile(){//手机版 帖子
		global $_G;
		return '';	//free
	}
}

?>